package cn.zlj.pethome.basic.util;

import cn.zlj.pethome.basic.domain.Logininfo;
import cn.zlj.pethome.basic.jwt.*;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request){
        //在header中获取jwt字符串
        String token = request.getHeader("token");
        try {
            //解析jwt
            PublicKey   publicKey = RsaUtils.getPublicKey(JwtRasHolder.INSTANCE.getJwtRsaPubData());
            Payload<UserInfo> payload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
            UserInfo userInfo = payload.getUserInfo();
            if(userInfo==null){
                return null;
            }
            return userInfo.getLogininfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
      return null;
    }
}
